from flask import g, Blueprint, request, jsonify, render_template, session, redirect
from models import Classes, Teacher
from exts import db, get_time
from flask_login import login_required

bp = Blueprint("classes", __name__, url_prefix="/classes")


# todo before_request
@bp.before_request
@login_required
def before_request():
    pass


# todo /add_classes
@bp.route("/add_classes", methods=["POST"])
def add_classes():
    name = request.values.get("name")
    room = request.values.get("room")
    time = request.values.get("time")
    department = request.values.get("department")
    id_teacher = request.values.get("teacher")
    # print(id_teacher)

    classes = Classes(
        name=name,
        room=room,
        time=time,
        department=department,
        create_time=get_time(),
        update_time=get_time(),
        is_delete=0,
        teacher=db.session.query(Teacher).get(id_teacher)
    )

    db.session.add(classes)
    db.session.commit()

    return redirect("/admin/add_classes_url")
